
const webpack = require('webpack')
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
class MyPlugin {
    apply (compiler) {
        console.log('Myplugin 启动')
        compiler.hooks.emit.tap('MyPlugin', compilation => {
            // 遍历编译后的文件，移除js文件中的注释
            for (const name in compilation.assets) {
                if (name.endsWith('.js')) {
                    const contents = compilation.assets[name].source();
                    const formatContents = contents.replace(/\/\*\*+\*\//g, '');
                    compilation.assets[name] = {
                        source: () => formatContents,
                        size: () => formatContents.length,
                    }
                }
            }
        })
    
    }
}
module.exports = {
    mode: 'none',
    entry: './src/main.js',
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, 'dist'),
        // publicPath: 'dist/'
    },
    devServer: {
        contentBase: ['./public'],
        proxy: {
            '/api': {
                // http://localhost:8080/api/user -> https://api.github.com/api/users
                target: 'https://api.github.com',
                pathRewrite: {
                    '^/api': '',
                },
                // 不能使用localhost:8080 作为请求github的主机名
                changeOrigin: true,
            }
        },
        hotOnly: true,
    },
    optimization: {
        // usedExports: true,
        // concatenateModules: true,
        sideEffects: true,
    },
    devtool: 'source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            '@babel/preset-env'
                            // ['@babel/preset-env', { modules: 'commonjs' }]
                        ]
                    }
                }
            },
            {
                test: /\.md$/,
                use: [
                    'html-loader',
                    './marked.js'
                ]
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ]
            },
            {
                test: /[\.jpeg|\.png]$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 2*1024,
                    }
                }
            }
        ],
    },
    plugins: [
        new webpack.DefinePlugin({
            API_BASE_URL: JSON.stringify('http://api.dev.com')
        }),
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: '史帅超',
            meta: {
                viewport: 'width=device-width'
            },
            template: './src/index.html',
        }),
        // 
        new HtmlWebpackPlugin({
            filename: 'about.html',
        }),
        // 开发时最好不要用，浪费打包时间
        // new CopyWebpackPlugin({
        //     patterns: [
        //         'public'
        //     ]
        // }),
        // new MyPlugin(),
    ]
}